//
//  NEDConstant.m
//  ScriptKill
//
//  Created by lin on 2020/6/22.
//  Copyright © 2020 ScriptKill. All rights reserved.
//

#import "NEDConstant.h"




BOOL NEDIsIPad(void){
    return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
}


static CGFloat ned_tatio = 0;
CGFloat Adapted(CGFloat value){
//    CGFloat v = ceilf(ned_tatio*value);
//    v = MAX(v, value);
    return AdaptedScaleWidth()*value;
}
CGFloat AdaptedScaleWidth(void){
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (ned_tatio==0) {
        if(NEDIsIPad()){
            ned_tatio = (width>height?height:width)/768.0;//9.7
        }else{
            ned_tatio = (width>height?height:width)/375.0;//4.7
        }
    }
    return ned_tatio;
}
static CGFloat ned_tatio_height = 0;
CGFloat AdaptedHeight(CGFloat value){
    return AdaptedScaleHeight()*value;
}
CGFloat AdaptedScaleHeight(void){
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (ned_tatio_height==0) {
        if(NEDIsIPad()){
            ned_tatio_height = (width>height?width:height)/1024.0;//9.7
        }else{
            ned_tatio_height = (width>height?width:height)/667.0;//4.7
        }
    }
    return ned_tatio_height;
}



#pragma mark - notifition

/// 登录信息 Token过期
NSNotificationName NEDLoginTokenExpireNotifition = @"kNEDLoginTokenExpireNotifition";

/// 登录状态变化
NSNotificationName NEDLoginStatusChangeNotifition = @"kNEDLoginStatusChangeNotifition";

/// 用户信息变化
NSNotificationName NEDLoginUserInfoChangeNotifition = @"kNEDLoginUserInfoChangeNotifition";

#pragma mark - null


BOOL IsNull(id obj){
    if(!obj){
        return YES;
    }
    if(obj == nil || [obj isEqual:[NSNull class]] || [obj isKindOfClass:[NSNull class]]){
        return YES;
    }
    if([obj isKindOfClass:[NSString class]]){
        NSString * str = (NSString *)obj;
        if([str isEqualToString:@""]){
            return YES;
        }
        if ([[str stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]){
            return YES;
        }
    }
    
    return NO;
}
BOOL IsNonNull(id obj){
    return !IsNull(obj);
}

BOOL IsEmpty(id obj){
    if(IsNull(obj)){
        return YES;
    }
    if([obj isKindOfClass:[NSArray class]]){
        NSArray * list = (NSArray *)obj;
        BOOL isNull = NO;
        for (NSObject * object in list) {
            if ([object isKindOfClass:NSString.class]) {
                BOOL classType = IsNull(object);
                if (!isNull && classType) {
                    isNull = YES;
                }
                
            }else if ([object isKindOfClass:NSNull.class]) {
                if (!isNull) {
                    isNull = YES;
                }
            }
        }
        if (!isNull) {
            return list.count == 0;
        }else{
            return isNull;
        }
    }
    if([obj isKindOfClass:[NSDictionary class]]){
        NSDictionary * dic = (NSDictionary *)obj;
        return dic.count == 0;
    }
    if([obj isKindOfClass:[NSString class]]){
        NSString * str = (NSString *)obj;
        return str.length == 0;
    }
    if([obj isKindOfClass:[NSSet class]]){
        NSSet * set = (NSSet *)obj;
        return set.count == 0;
    }
    if([obj isKindOfClass:[NSData class]]){
        NSData * data = (NSData *)obj;
        return data.length == 0;
    }
    return NO;
}
BOOL IsNotEmpty(id obj){
    return !IsEmpty(obj);
}


/// 如果是空的显示 @"" 空的字符串
NSString * NullShow(id obj){
    if(IsNull(obj)){
        return @"";
    }
    if(![obj isKindOfClass:[NSString class]]){
        return [NSString stringWithFormat:@"%@",obj];
    }
    return obj;
}
/// 如果是空的显示 - 替代
NSString * NullShowReplaceLine(id obj){
    if(IsNull(obj)){
        return @"-";
    }
    if(![obj isKindOfClass:[NSString class]]){
        return [NSString stringWithFormat:@"%@",obj];
    }
    return obj;
}


/// MARK: - Log

void NEDLog(NSString * type, NSString * className, NSInteger lineNum, NSString * format, ...){
#ifdef DEBUG
//__func__    %s    打印当前函数名
//__LINE__    %d    源码文件中的行号
//__FILE__    %s    源码文件完整路径
//__PRETTY_FUNCTION__    %s    和__func__类似, 但是在 C++ 代码中包含更多的信息.
    format = [NSString stringWithFormat:@"[%@,%ld]%@: %@",className,lineNum,type,format];
    
    va_list argptr;
    va_start(argptr, format);
    NSLogv(format, argptr);
    va_end(argptr);
#endif
}


@implementation NEDConstant

@end
